package com.waypolice.manager.service.impl;

import com.waypolice.manager.service.MainMenuService;
import com.waypolice.manager.service.PermissionsService;
import com.waypolice.mapper.UpmsApiMapper;
import com.waypolice.mapper.UpmsPermissionMapper;
import com.waypolice.mapper.UpmsRolePermissionMapper;
import com.waypolice.mapper.UpmsUserRoleMapper;
import com.waypolice.pojo.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 首页左侧导航菜单接口实现
 * @author zhangxiaoyang
 *
 */
@Service
public class MainMenuServiceImpl implements MainMenuService{
	@Autowired
	private UpmsPermissionMapper permissionsMapper;

	@Autowired
	private UpmsUserRoleMapper upmsUserRoleMapper;

	@Autowired
	private UpmsRolePermissionMapper upmsRolePermissionMapper;

	@Autowired
	private UpmsApiMapper upmsApiMapper;

	@Override
	public List<Map<String, Object>> getMainMenu(Integer id) {
		UpmsPermissionExample upmsPermissionExample = new UpmsPermissionExample();
		upmsPermissionExample.setOrderByClause("\"ORDERS\" ASC,\"SYSTEM_ID\" ASC");
		List<UpmsPermission> upmsPermissions = upmsApiMapper.selectUpmsPermissionByUpmsUserId(id);
		//List<UpmsPermission> upmsPermissions = permissionsMapper.selectByExample(upmsPermissionExample);
		return navBarList(upmsPermissions);
	}
	public List<Map<String, Object>> navBarList(List<UpmsPermission> navBar){
		List<Map<String, Object>> obj = new ArrayList<>();
		for (UpmsPermission res : navBar) {
			HashMap<String, Object> mapArr = new HashMap<>();
            Integer reUkId = res.getPermissionId();
            mapArr.put("menuid",reUkId.toString());
            mapArr.put("icon",res.getIcon());
            mapArr.put("menuname",res.getName());
            mapArr.put("url",res.getUri());
            mapArr.put("fatherPoint",res.getPid());
			mapArr.put("orders",res.getOrders());

            UpmsPermissionExample upmsPermissionExample = new UpmsPermissionExample();
            UpmsPermissionExample.Criteria criteria = upmsPermissionExample.createCriteria();
            criteria.andPidEqualTo(reUkId);
            ArrayList<Short> shorts = new ArrayList<>();
            shorts.add((short) 1);
            shorts.add((short) 2);
            criteria.andTypeIn(shorts);
            List<UpmsPermission> upmsPermissions = permissionsMapper.selectByExample(upmsPermissionExample);
            if (upmsPermissions.size()!=0){
                mapArr.put("isJump",1);
            }else {
                mapArr.put("isJump",0);

            }
            obj.add(mapArr);
		}
		return obj;
	}

}
